package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌管理接口管理
 * 测试git分支
 */
@Slf4j
@Tag(name = "品牌管理接口管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    IBrandService brandService;

    @RequiresPermissions("product:brand:list")
    @Operation(summary = "分页")
    @GetMapping("list")
    public TableDataInfo list(Brand brand){
        startPage(); //获取HttpServletRequest获取请求参数pageNum,pageSize,封装成Page对象，通过ThreadLocal绑定到线程上。
        List<Brand> brandList =  brandService.list(brand);
        return getDataTable(brandList);
    }

    @Operation(summary = "根据ID查询品牌对象")
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable("id") Long id){
        Brand brand = brandService.getById(id);
        return success(brand);
    }

    @Log(title = "品牌管理", businessType = BusinessType.INSERT)
    @Operation(summary = "新增")
    @PostMapping
    public AjaxResult save(@RequestBody @Validated Brand brand){ // 注意：@RequestBody 注解别导错包
        brand.setCreateBy(SecurityUtils.getUsername()); //利用alibaba提供的TransmittableThreadLocal获取线程上绑定的map，从map中获取用户名称等信息。
        //brand.setCreateTime(new Date());
        int rows = brandService.save(brand);
        return toAjax(rows);
    }

    @Log(title = "品牌管理", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改")
    @PutMapping
    public AjaxResult update(@RequestBody @Validated Brand brand){
        brand.setUpdateBy(SecurityUtils.getUsername());
        int rows = brandService.updateById(brand);
        return toAjax(rows);
    }

    @Log(title = "品牌管理", businessType = BusinessType.DELETE)
    @Operation(summary = "根据id删除一条或多条品牌数据")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteBatch(@PathVariable("ids") Long[] ids){
        return toAjax(brandService.deleteBatch(ids));
    }

    @Operation(summary = "查询所有品牌")
    @GetMapping("getBrandAll")
    public AjaxResult getBrandAll(){ // AjaxResult  extends HashMap
        List<Brand> brandList =  brandService.getBrandAll();
        return success(brandList);
    }

    @InnerAuth
    @Operation(summary = "获取全部品牌")
    @GetMapping("getBrandAllList")
    public R<List<Brand>> getBrandAllList() {
        return R.ok(brandService.getBrandAll());
    }
}
